package com.algorithm.find;

/**
 * @description: PictureExtractionTools 图片抽取工具类
 * @author chenqb
 * @date 2021/7/28 17:01
 * @version 1.0
 */
import java.io.*;
import java.util.ArrayList;
import java.util.List;

public class PictureExtractionTools {
    public static void main(String[] args) throws IOException {
        PictureExtractionTools pictureExtractionTools=new PictureExtractionTools();
        //1.fileOutputPath指定过滤后的照片存储位置
        String fileOutputPath="D:\\out";
        //2.findNames，图片处理后生成的txt文件，根据txt前缀名获取要找的图片名称集合。
        String findNames="G:\\cqb\\callbackData";
        //3.图片源文件存储位置
        String sourceFilePath="G:\\cqb\\UAV-benchmark-M";
        pictureExtractionTools.pictureCopySelectionMethod(findNames,fileOutputPath,sourceFilePath);
    }

    /**
     * @description 图片抽取工具主方法
     * @param fileOutputPath 文件输出路径
     * @param sourceFilePath 源文件路径
     * @param findNames 对应的要获取的文件名称
     * @return void
     * @author chenqb
     * @date 2021/7/28 17:22
     */
    public void pictureCopySelectionMethod(String findNames,String fileOutputPath,String sourceFilePath) throws IOException {
        //1.获取要处理的图片的所有文件夹名称，放入list集合
        List<String> list=getFileName(findNames);
        System.out.println(list);
        //2.遍历list集合，一个文件夹一个文件夹处理
        for(String s: list){
            this.selectPicture(getFileName(findNames+"\\"+s),fileOutputPath,sourceFilePath,s);
        }
    }

     List<String> getFileName(String sourceFilePath) {
        List<String> ss=new ArrayList<>();
        String path = sourceFilePath;
        File f = new File(path);
        if (!f.exists()) {
            System.out.println(path + " not exists");
        }

        File[] fa = f.listFiles();
        for (int i = 0; i < fa.length; i++) {
            File fs = fa[i];
            if (fs.isDirectory()) {
                System.out.println(fs.getName());
                ss.add(fs.getName());
            } else {
                if (!fs.getName().equals("classes.txt")) {
                    String str1 = fs.getName().substring(0, fs.getName().indexOf("."));
                    System.out.println(fs.getName());
                    ss.add(str1 + ".jpg");
                }
            }
        }
        return ss;
    }

     void selectPicture(List<String> strings,String fileOutputPath,String sourceFilePath,String folderName) throws IOException {
        String folderPath= fileOutputPath+"\\"+folderName;
        File folder = new File(folderPath);
        if (!folder.exists() && !folder.isDirectory()) {
            folder.mkdirs();
            System.out.println("创建文件夹");
        } else {
            System.out.println("文件夹已存在");
        }
        for (String s : strings) {
            FileInputStream fis = new FileInputStream(sourceFilePath+"\\"+folderName+"\\"+s);
            BufferedInputStream bufis = new BufferedInputStream(fis);

            FileOutputStream fos = new FileOutputStream(fileOutputPath+"\\"+folderName+"\\"+s);
            BufferedOutputStream bufos = new BufferedOutputStream(fos);

            int len = 0;
            while ((len = bufis.read()) != -1) {
                bufos.write(len);
            }
            bufis.close();
            bufos.close();
        }
    }
}